package com.wuhao.code.check.completion

import com.aegis.mybatis.xmlless.annotations.XmllessMethod
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.psi.*
import com.intellij.psi.util.parentOfType
import com.intellij.util.ProcessingContext

/**
 * @author 吴昊
 * @date 2024/02/01
 * @version 1.0
 * @since v1.0.0
 */
class JavaReferenceContributor : BaseXmllessReferenceContributor() {

  override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
    registrar.registerReferenceProvider(
        psiElement(PsiLiteralExpression::class.java)
            .withSuperParent(
                2,
                psiElement(PsiNameValuePair::class.java)
                    .withFirstChild(
                        psiElement(PsiIdentifier::class.java)
                            .withText("conditions")
                    )
            )
            .withSuperParent(
                4, psiElement(PsiAnnotation::class.java)
            ),
        object : PsiReferenceProvider() {

          override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> {
            element.parentOfType<PsiMethod>() ?: return PsiReference.EMPTY_ARRAY
            val anno = element.parentOfType<PsiAnnotation>()
            if (anno?.qualifiedName != XmllessMethod::class.java.name) {
              return PsiReference.EMPTY_ARRAY
            }
            val literalExpression = element as PsiLiteralExpression
            val value = literalExpression.text ?: return PsiReference.EMPTY_ARRAY
            return create(value, element)
          }

        }
    )
  }

}
